const lodash = require('lodash')
const loginFns = require('../common/login')
import { pageFns } from '../utils/pageHelper'
import { funcFns } from '@/puppeteer/utils/funtion'

import { SourceDao } from '@/dao/sourceDao'
import { ArticleDao } from '@/dao/articleDao'
import { ArticleStatus } from '@/entities/Constant'

const SELECTORS = { "IMAGES": [".carousel-container .swiper-slide img"], "TITLE": [".description-container .description-left .goods-name"], "PRICE": [".price-container .price-left .price"] }
async function crawleProduct(source, folderPath, page, selectors) {
  try {
    let res = await pageFns.runResetTimout(
      source.link,
      page,
      async (link, page) => {
        await page.goto(link, { waitUntil: 'networkidle0' })
        return { err: null }
      },
      { page: page, timeout: 60 * 1000 }
    )
    if (res.err) {
      return res
    }
    /**
     * @description Crawle item list
     */
    res = await pageFns.deepRetry(
      selectors.IMAGES,
      page,
      async (selector, page) => {
        await page.waitForSelector(selector)
        return await pageFns.attributeNames(page, selector, 'src')
      },
      { page: page, timeout: 60 * 1000 }
    )
    if (res.err) {
      return res
    }
    console.log('attributes', res)
    /**
     * @description start to download file
     */
    for (let i = 0; i < res.data.length; i++) {
      let src = res.data[i]
      await funcFns.downloadFileWithFileName(src, folderPath, i)
    }
    console.log('finish downloading...', res.data.length, res.data)
    /**
     * @description get product title
     */
    res = await pageFns.deepRetry(
      selectors.TITLE,
      page,
      async (selector, page) => {
        await page.waitForSelector(selector)
        return await pageFns.getText(page, selector)
      },
      { page: page, timeout: 500 }
    )
    if (res.err) {
      return res
    }
    let content = { title: res.data, priceItems: [], attributeItems: {} }
    let article = {
      id: 0,
      title: res.data,
      link: source.link,
      sourceId: source.id,
      platformId: source.platform,
      status: ArticleStatus.Gather,
    }
    res = await pageFns.deepRetry(
      selectors.PRICE,
      page,
      async (selector, page) => {
        await page.waitForSelector(selector)
        return await pageFns.getText(page, selector)
      },
      { page: page, timeout: 500 }
    )
    if (res.err) {
      return res
    }
    content.priceItems.push(res.data)
    article.content = JSON.stringify(content)
    let id = await ArticleDao.save(article)
    if (!id) {
      return { err: 'ArticleDao保存失败', data: article }
    }
    article.id = id
    return { err: null, data: article }
  } catch (err) {
    return { err: err, data: null }
  }
}

const LOGINS = {}

async function goSpider(page, task, source, rule) {
  let selectors = JSON.parse(rule.codeRule)
  if (lodash.isEmpty(selectors)) {
    return { err: 'there is not selectors of source:' + JSON.stringify(source) }
  }
  try {
    //是否需要登录
    if (task.accountId) {
      let res = await loginFns.checkLogin(page, task, LOGINS)
      if (res.err) {
        console.log('goSpider.checkLogin', res)
        return res
      }
    }
    return await crawleProduct(
      source,
      SourceDao.folderPath(source),
      page,
      selectors
    )
  } catch (err) {
    return { err: err }
  }
}

export { goSpider }
